package adv5;

import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;

/**
 * @author oldliu
 * @since 1.0
 */
public class FileSize {
    //非递归的方案
    public static long getSize2(File f){
        LinkedList<File> files=new LinkedList<>();
        files.add(f);
        long size=0;
        while(!files.isEmpty()){
            File temp=files.removeFirst();
            if(temp.isFile()){
                size+=temp.length();
                continue;
            }
            File[] fr = temp.listFiles();
            files.addAll(Arrays.asList(fr));//集合加上另一个集合中
        }
        return size;
    }
    //文件夹不算大小
    public static long getSize(File f){
        long size=0;
        if(f.isDirectory()){
            File[] files = f.listFiles();
            for(File f1:files){
                size+=getSize(f1);
            }
            return size;
        }
        return f.length();
    }



    public static void b(){
        System.out.println("This is b");
        long size=0;

        //return size;
    }

    public static void a(){
        System.out.println("This is a ");
        a();
    }
    //斐波拉契数列的第k项
    public static long feibolaqie(int k){
        if(k<=2)
            return 1L;
        return feibolaqie(k-1)+feibolaqie(k-2);
    }
    //阶乘 n!=1*2*3..n
    //斐波拉契数列 1，1，2，3.。。。
    //n!=n*(n-1)!
    //

    public static void main(String[] args) {
       // a();
        File f=new File("D:\\资料-java");
        long start=System.nanoTime();
        long size=getSize(f);
        long end=System.nanoTime();
        System.out.println("size:"+size);
        long start2=System.nanoTime();
        System.out.println("size:"+getSize2(f));
        long end2=System.nanoTime();
        System.out.println(end-start);
        System.out.println(end2-start2);
//        long size=0;
//        if(f.isDirectory()){
//            File[] fr = f.listFiles();
//            for (File f1:fr){
//                if(f1.isFile())
//                    size+=f1.length();
//            }
//        }
//        System.out.println(size);
    }
}
